#include<iostream>
#include<pthread.h>

class Mutex//为一条龙解决锁的问题，自动析构解锁
{ public:
    Mutex(pthread_mutex_t *mutex=nullptr):_mutex(mutex)//因为加锁是用的指针变量，因此传递指针
    {}
  void lock()
  {if(_mutex)
     pthread_mutex_lock(_mutex);
  }
  void unlock()
  { if(_mutex)
    pthread_mutex_unlock(_mutex);
  }
  ~Mutex(){}


private:
 pthread_mutex_t *_mutex;
};

class Lockguard
{
  public:
  Lockguard(pthread_mutex_t *p_lock):_lock(p_lock)//传递指针，用指针进行创建类对象
  { 
    _lock.lock();
  }

  ~Lockguard()
  {
    _lock.unlock();
  }
  private:
  Mutex _lock;
};